package org.example.j2024_11_12_2;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/book")
public class BookController {



    @RequestMapping("/getList")
    public List<BookInfo> getList(){

         // 因为暂时数据库上没有东西,
        // 用Mock 的方法 ,提供虚拟的数据
        List<BookInfo>  bookInfos= mockBookList() ;
        for (BookInfo book : bookInfos) {
              // 根据状态码设置 一下中文的状态码
            // 方便前端显示状态
            if(book.getStatus() == 1){
                book.setStatusCN("不可借阅");
            }else{
                book.setStatusCN("可借阅");
            }
        }

        return   bookInfos;
    }

    private List<BookInfo> mockBookList() {   // mock 构造虚拟数据
        List<BookInfo> list  = new ArrayList<>();
        for (int i = 1; i <= 15; i++) {
               BookInfo book = new BookInfo() ;
               book.setId(i);
               book.setBookName("图书"+i);
               book.setAuthor("作者"+i);
               book.setPublisher("出版社"+i);
               book.setCount(i);
               book.setPrice(new BigDecimal(new Random().nextInt(100)));
               book.setStatus(i%4==0?1:2);
               list.add(book);
        }
        return list ;
    }
}
